import request from '@/utils/request';
import type { PageResponse, Result } from '@/types/global';

// 充电桩接口
export interface ChargingPileItem {
  id: number;
  code: string;        // 充电桩编号
  name: string;        // 充电桩名称
  createDate: number;  // 创建时间
  updateDate: number;  // 更新时间
  status: number;      // 状态
  chargingGroupId: number;  // 充电站点ID
  deviceType: number;       // 设备类型
  enableFlag: number;       // 启用状态
  socketCount: number;      // 插座数量
  totalPower: number;       // 总功率
  protocolType: number;     // 协议类型
  volume: number;          // 音量
  maxTemperature: number;   // 最大温度
  startupVoice: string;    // 启动语音
  guns?: GunItem[];        // 充电枪列表
}

// 充电枪接口
export interface GunItem {
  id: number;
  createDate: number;
  updateDate: number;
  code: string;
  status: number;
  timeTotal: number;
  powerTotal: number;
  chargingId: number;
  lastUseTime: number;
  memberId: number;
  orderNumber: string;
  orderCreateDate: number;
  socketName: string;
}

// 服务器接口
export interface ForwardServer {
  id: number;
  serverName: string;
  serverIp: string;
  serverPort: number;
  serverType: number;
  status: number;
  remark: string;
}

// 服务器关联接口
export interface ServerRelationItem {
  id?: number;
  serverId: number;
  pileId: number;
  forwardPileCode: string;
  isPrimary: number;
  connectionStatus: number;
  createTime?: string;
  updateTime?: string;
}

// 分页请求参数
export interface PageParams {
  current: number;
  size: number;
  pileName?: string;
  pileType?: number;
  status?: number;
  chargingGroupId: number | null;
}

// 获取充电桩列表
export function getChargingPileList(params: PageParams) {
  return request.get<PageResponse<ChargingPileItem>>('/api/charging/list', params);
}

// 新增充电桩
export function addChargingPile(data: Partial<ChargingPileItem>) {
  return request.post<{ code: number; message: string; data: { id: number } }>('/api/charging/pile', data);
}

// 编辑充电桩
export function updateChargingPile(id: number, data: Partial<ChargingPileItem>) {
  return request.put<{ code: number; message: string }>(`/api/charging/pile/${id}`, data);
}

// 获取充电桩详情
export function getChargingPileDetail(id: number) {
  return request.get<ChargingPileItem>(`/api/charging/pile/${id}`);
}

// 获取充电枪列表
export function getGunList(chargingId: number) {
  return request.get<GunItem[]>(`/api/charging/socket/list?id=${chargingId}`);
}

// 获取充电枪详情
export function getGunDetail(id: number) {
  return request.get<GunItem>(`/api/charging/socket/${id}`);
}

// 新增充电枪
export function addGun(data: Partial<GunItem>) {
  return request.post('/api/charging/socket', data);
}

// 编辑充电枪
export function updateGun(data: Partial<GunItem>) {
  return request.put('/api/charging/socket', data);
}

// 删除充电枪
export function deleteGun(id: number) {
  return request.delete(`/api/charging/socket/${id}`);
}

// 获取转发服务器列表
export function getForwardServerList() {
  return request.get<ForwardServer[]>('/api/forward/server/list');
}

// 获取充电桩的服务器关联列表
export function getPileServerRelations(pileId: number) {
  return request.get<ServerRelationItem[]>(`/api/pile/server/relation/pile/${pileId}`);
}

// 创建服务器关联
export function createServerRelation(data: Partial<ServerRelationItem>) {
  return request.post<{ code: number; message: string }>('/api/pile/server/relation', data);
}

// 更新服务器关联
export function updateServerRelation(id: number, data: Partial<ServerRelationItem>) {
  return request.put<{ code: number; message: string }>(`/api/pile/server/relation/${id}`, data);
}

// 删除服务器关联
export function deleteServerRelation(id: number) {
  return request.delete<{ code: number; message: string }>(`/api/pile/server/relation/${id}`);
}

// 获取充电枪实时数据
export function getGunRealtimeData(id: number) {
  return request.get<Result<GunItem>>(`/api/charging/socket/realtime/${id}`);
} 